package cn.kgc.ubagnet.controller;

import cn.kgc.ubagnet.pojo.Cart;
import cn.kgc.ubagnet.pojo.CartPro;
import cn.kgc.ubagnet.pojo.Order;
import cn.kgc.ubagnet.pojo.Orderdetails;
import cn.kgc.ubagnet.service.buyer.BuyerService;
import cn.kgc.ubagnet.service.buyer.CrowdBuyerDetailService;
import cn.kgc.ubagnet.service.cart.CartService;
import cn.kgc.ubagnet.service.order.OrderDetailsService;
import cn.kgc.ubagnet.service.order.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class CartController {
    @Resource
    CartService cartService;
    @Resource
    OrderService orderService;
    @Resource
    OrderDetailsService orderDetailsService;
    @Resource
    BuyerService ouyerService;

    //显示购物车
    @GetMapping("/cart/list")
    @ResponseBody
    public List<CartPro> cartprolist(@RequestParam(value = "checkid",required = false,defaultValue = "0") Integer checkid, HttpSession session){
        Integer userid=ouyerService.seleByPhone(CrowdBuyerDetailService.phone).getUserid();
        System.out.println(userid);
        List<CartPro> list=cartService.CarttProList(userid,checkid);
        if(list.size()>0){
            return list;
        }else{
            return null;
        }
    }

    //添加购物车
    @PostMapping("/cart/add")
    @ResponseBody
    public Cart AddCart(@RequestBody Cart cart, HttpSession session){
        String chars = "0123456789";
        char[] rands = new char[6];
        for (int i = 0; i < 6; i++) {
            int rand = (int) (Math.random() * 10);
            rands[i] = chars.charAt(rand);
        }
        cart.setCartid(rands.toString());
        //获取用户id
        Integer userid=ouyerService.seleByPhone(CrowdBuyerDetailService.phone).getUserid();
        cart.setUserid(userid);
        cart.setTotalprice(cart.getPrice()*cart.getNum());
        int result=cartService.AddCart(cart);
        if(result>0) return cart;
        else return null;
    }

    //修改购物车
    @PostMapping("/cart/save")
    @ResponseBody
    public Map<String,Object> saveCart(@RequestBody List<CartPro> crlist, HttpSession session){
        Map<String,Object> map=new HashMap<>();
        //生成详细订单号
        String val = "";
        Random random = new Random();
        for (int i = 0; i < 16; i++) {
            val += String.valueOf(random.nextInt(10));
        }
        session.setAttribute("orderdetailid",Long.parseLong(val));
        //订单添加成功次数
        int yesnum=0;
        //购物车编号,list
        List<Integer> idlist=new ArrayList<>();
        //一遍一遍的将购物车信息添加到订单
        for (int i=0;i<crlist.size();i++){
            CartPro cart=crlist.get(i);
            idlist.add(cart.getId());
            Order order=new Order();
            order.setIscoupon(0);   //0未使用优惠卷,1使用优惠卷
            order.setClothesid(cart.getProid());//商品编号
            order.setCount(cart.getNum());//商品数量
            order.setOrderdetailsid(Long.parseLong(val));//订单详细编号
            order.setPrice(cart.getPrice());//价钱
            order.setTotalprice(cart.getPrice()*cart.getNum());//总价
            order.setColor(cart.getColor());//服装颜色
            order.setSize(cart.getSize());//服装尺寸
            order.setStatus(1);//待支付
            int result=orderService.Ordersave(order);//添加订单
            if(result>0) yesnum++;
        }
        if(yesnum==crlist.size()){//全部添加成功
            //删除购物车
            cartService.delCart(idlist);
            //添加订单详情
            Orderdetails od=new Orderdetails();
            od.setAddressid(null);
            od.setCreatedate(new Date());
            od.setRealitypay(0.0);
            od.setId(Long.parseLong(val));


            //获取用户id
            Integer userid=ouyerService.seleByPhone(CrowdBuyerDetailService.phone).getUserid();
            od.setUserid(userid);
            od.setStatus(0);
            int i = orderDetailsService.Add_OD(od);
            if(i>0){
                map.put("status","yes");
            }else{
                map.put("status","no");
            }
        }
        return map;
    }

    //添加到订单
    @PostMapping("/cart/add/order")
    @ResponseBody
    public int CartAddOrder(@RequestBody CartPro cart,HttpSession session){
        //生成详细订单号
        String val = "";
        Random random = new Random();
        for (int i = 0; i < 16; i++) {
            val += String.valueOf(random.nextInt(10));
        }
        session.setAttribute("orderdetailid",Long.parseLong(val));
        Order order=new Order();
        order.setIscoupon(0);   //0未使用优惠卷,1使用优惠卷
        order.setClothesid(cart.getProid());//商品编号
        order.setCount(cart.getNum());//商品数量
        order.setOrderdetailsid(Long.parseLong(val));//订单详细编号
        order.setPrice(cart.getPrice());//价钱
        order.setTotalprice(cart.getPrice()*cart.getNum());//总价
        order.setColor(cart.getColor());//服装颜色
        order.setSize(cart.getSize());//服装尺寸
        order.setStatus(1);//待支付
        orderService.Ordersave(order);

        //添加订单详情
        Orderdetails od=new Orderdetails();
        od.setAddressid(null);
        od.setCreatedate(new Date());
        od.setRealitypay(0.0);
        od.setId(Long.parseLong(val));
        //获取用户id
        Integer userid=ouyerService.seleByPhone(CrowdBuyerDetailService.phone).getUserid();
        od.setUserid(userid);
        od.setStatus(0);
        orderDetailsService.Add_OD(od);
        return 1;
    }

    //单(多)列删除购物车
    @PostMapping("/cart/del")
    @ResponseBody
    public int delCart(@RequestBody List<Integer> idlist){

        if(cartService.delCart(idlist)>0) return 1;
        return 0;
    }
}
